package com.facade;

import java.util.List;

import com.dao.UserDAO;
import com.model.User;

public class UserFacade {
	private UserDAO userDAO = new UserDAO();

	public User find(String username, String password) {
		userDAO.beginTransaction();
		User user = userDAO.findUserByUsernamePassword(username, password);
		userDAO.closeTransaction();
		if (user == null || !user.getPassword().equals(password)) {
			return null;
		}

		return user;
	}
	
	public User findLoggedUser(String username) {
		userDAO.beginTransaction();
		User user = userDAO.findUserByUsername(username);
		userDAO.closeTransaction();

		return user;
	}	
	
	public User find(int id){
		userDAO.beginTransaction();
		User user = userDAO.find(id);
		userDAO.closeTransaction();
		
		return user;
	}
	
	public List<User> list(){
		userDAO.beginTransaction();
		List<User> users = userDAO.findAll();
		userDAO.closeTransaction();
		
		return users;
	}
	
	public void create(User user){
		userDAO.beginTransaction();
		userDAO.save(user);
		userDAO.commitAndCloseTransaction();
		
	}
	
	
}